﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class DeliverySelectArea : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentAccount.GetUser().UserID.Substring(0, 2).ToUpper().Equals("QP") && Convert.ToInt32(CurrentAccount.GetUser().UserID.Substring(2, 4)) >= 435 || CurrentAccount.GetUser().UserID.ToUpper().Equals("QP0374"))
                {
                    btnAdd.Enabled = true;
                }
                BindArea();
            }
        }

        public void BindArea()
        {
            string userid = CurrentAccount.GetUser().UserID;//配送企业的id
            IList<Area> areaList = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().GetUnChoseArea(userid);
            this.gvwArea.DataSource = areaList;
            this.gvwArea.DataBind();

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //获取所有选择的地区id
            List<string> areaIds = new List<string>();
            

            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    areaIds.Add(this.gvwArea.DataKeys[_index].Value.ToString());
                }
            }

            if (areaIds.Count>0)
            {
                string userid = CurrentAccount.GetUser().UserID;
                bool flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().AddChoseArea(areaIds, userid);
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('选择成功！');window.location.href='DeliverySelectAreaManage.aspx';</script>"));
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<script>alert('选择失败或该地区已被选择！');</script>"));
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选定一个配送区域！", this);
            }
        }
    }
}
